/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#include "welcome.h"

#include "mapview.h"
#include <QTimer>

#include <QDesktopServices>

#include <QWebFrame>

Welcome::Welcome(MapView *parent) : QGraphicsWebView()
{
    mapView = parent;

    // connect(mapView,SIGNAL(mapRectChanged(QRect,int)),this,SLOT(mapRectHasChanged(QRect,int)));


    connect(this,SIGNAL(linkClicked(QUrl)),this,SLOT(linkWasClicked(QUrl)));

    connect(this,SIGNAL(loadFinished(bool)),this,SLOT(loadHasFinished(bool)));

    visible = false;

    // change this for on-line
    isOnline = false;

    this->setZoomFactor(0.8);
    this->setPos(-70,10);

    this->setRenderHints(QPainter::SmoothPixmapTransform);
    this->setResizesToContents(true);

     QWebFrame* frame = this->page()->mainFrame();
     frame->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
     frame->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );

     this->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);

     addWelcome();

}

void Welcome::removeWelcome()
{
    if (visible==true){
        mapView->getScene()->removeItem(this);
        visible = false;
    }
}

void Welcome::addWelcome()
{

    if (visible==false){


        mapView->setViewScale(100);
        mapView->setZoom(1);
        mapView->setCenterLonLat(QPointF(0,0));

        if (isOnline) this->load(QUrl("http://mapvision.filipzava.cz/welcome/welcome.html"));
        else this->load(QUrl("file:///C:/MapVision/welcome/welcome.html"));

        this->adjustSize();

        mapView->getScene()->addItem(this);

        this->setFocus(Qt::MouseFocusReason);


        visible = true;

    }
}

void Welcome::linkWasClicked(const QUrl & url)
{
    if (url.toString().contains("closingEvent"))
    {
        this->removeWelcome();
    } else {
        QDesktopServices::openUrl(url);
    }
}

void Welcome::mapRectHasChanged(QRect mapViewRect, int zoom)
{
  double mVScale = (double(mapView->getViewScale())/double(100));

    QRectF mapRect(mapView->mapToScene(mapView->rect().topLeft()),mapView->mapToScene(mapView->rect().bottomRight()));
    QSizeF mapViewSize=mapRect.size();

    this->setTransformOriginPoint(this->rect().center());

       if ((mapViewSize.height())<600/mVScale || (mapViewSize.width())<800/mVScale ) {

        if ((600 - mapViewSize.height()/mVScale)  > (800 - mapViewSize.width()/mVScale)){

            this->setZoomFactor((mapViewSize.height()/600)*mVScale-0.04);
        } else {

           this->setZoomFactor((mapViewSize.width()/800)*mVScale-0.04);
        }

    } else {
        this->setScale(1/mVScale);
    }

   QPointF center  = (mapView->mapToScene(mapView->rect().topLeft())+mapView->mapToScene(mapView->rect().bottomRight()))/2;

   this->setPos(center-this->rect().center());


}

void Welcome::loadHasFinished(bool isFinished)
{
    if (!isFinished){
        isOnline=false;
        this->removeWelcome();
        this->addWelcome();
    }
}

